package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.LISTAPROP;
import ar.edu.caece.compiler.grammar.nonterminals.PROP;
import ar.edu.caece.compiler.grammar.nonterminals.LISTAPROP1;

import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;

public class LISTAPROP1Factory extends AbstractTokenVisitorFactory {

	private static LISTAPROP1Factory INSTANCE;
	
	public static LISTAPROP1Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new LISTAPROP1Factory();
		return INSTANCE;
	}
	
	private PROPFactory propFactory;
	private LISTAPROPFactory listapropFactory;
	private TokenVisitorInterface parsePROP;
	private TokenVisitorInterface parseLISTAPROP;
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			propFactory = PROPFactory.getInstance();
			listapropFactory = LISTAPROPFactory.getInstance();
			parseLISTAPROP = listapropFactory.makeVisitor();
			parsePROP = propFactory.makeVisitor();
		}
	};
	
	private LISTAPROP1Factory() throws Exception {

	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador)
					throws Exception {
				GrammarSymbol result = llamador.execute(propFactory.makeChainedVisitor(siguiente));
				if(result instanceof PROP) {
					return new LISTAPROP1 ((PROP) result, (LISTAPROP) nextToken().execute(parseLISTAPROP)); 
				} 
				else {
					return result;
				}
			}
			
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				return new LISTAPROP1 ((PROP) llamador.execute(parsePROP), (LISTAPROP) nextToken().execute(parseLISTAPROP));
			}
		};
	}
	
	
}
